/************************************************************************************/
/*  Program name CAPGLIMP_BALANCES.sql                                        	    */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 10 FEB, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_BALANCES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_BALANCES;
		
	lv_COAID	NUMBER;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_COAID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);
			
			INSERT INTO GL_BALANCES
			VALUES
			(
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			CAPBRIMP_COMMON.GetSegment(lv_COAID,C1.CODE_COMB_ID_D),
			C1.CURRENCY_CODE,
			C1.PERIOD_NAME,
			C1.ACTUAL_FLAG,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetBudgetVerID(C1.BUDGET_NAME),
			CAPBRIMP_COMMON.GetEncumTypeID(C1.ENCUM_TYPE_NAME),
			C1.TRANSLATED_FLAG,
			C1.REVALUATION_STATUS,
			C1.PERIOD_TYPE,
			C1.PERIOD_YEAR,
			C1.PERIOD_NUM,
			C1.PERIOD_NET_DR,
			C1.PERIOD_NET_CR,
			C1.PERIOD_TO_DATE_ADB,
			C1.QUARTER_TO_DATE_DR,
			C1.QUARTER_TO_DATE_CR,
			C1.QUARTER_TO_DATE_ADB,
			C1.YEAR_TO_DATE_ADB,
			C1.PROJECT_TO_DATE_DR,
			C1.PROJECT_TO_DATE_CR,
			C1.PROJECT_TO_DATE_ADB,
			C1.BEGIN_BALANCE_DR,
			C1.BEGIN_BALANCE_CR,
			C1.PERIOD_NET_DR_BEQ,
			C1.PERIOD_NET_CR_BEQ,
			C1.BEGIN_BALANCE_DR_BEQ,
			C1.BEGIN_BALANCE_CR_BEQ,
			C1.TEMPLATE_ID,
			C1.ENCUMBRANCE_DOC_ID,
			C1.ENCUMBRANCE_LINE_NUM,
			C1.QUARTER_TO_DATE_DR_BEQ,
			C1.QUARTER_TO_DATE_CR_BEQ,
			C1.PROJECT_TO_DATE_DR_BEQ,
			C1.PROJECT_TO_DATE_CR_BEQ
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/